import Vue from 'vue'
import Router, {
    RouterMount
} from '@/js_sdk/hhyang-uni-simple-router';
import Store from '@/lib/store';
import qs from 'qs';
import Lib from './index'
import config from '@/lib/config' 

Vue.use(Router);

const routesConfig = {
    h5: {
        paramsToQuery: true,
        loading: true,
        vueRouterDev: false,
        useUniConfig: true,
        resetStyle: () => {
            return {
                style: `
				#router-loadding .loadding {
					background-color: #1079fc !important;
					box-shadow: 0 0 15px #1079fc !important;
				}
				`
            }
        }
    },
    APP:{
        loddingPageStyle:()=>JSON.parse('{"backgroundColor":"#96DBF3"}'),
        loddingPageHook:(view)=>{
            view.drawBitmap('/static/wait.gif', {}, {
                top: 'auto',
                left: 'auto',
                width: '200px',
                height: '200px'
            })
        },
        holdTabbarStyle:()=>JSON.parse('{"backgroundColor":"#FF0000","opacity":0.2}')
    },
    encodeURI: false,
    debugger: true,
    routerBeforeEach:()=>{
        // console.log('routerBeforeEach')
    },
    routerAfterEach:()=>{
        // console.log('routerAfterEach')
    },
	 routes:ROUTES //路由表

}


const router = new Router(routesConfig);
const whitelist = {
    '/pages/index/index': 'index',
}


router.beforeEach((to, from, next) => {
    if(Store.getters.login_Status == 0 ){
        if(Store.getters.appId == ''){
            let res = Lib.$http.post({
                url: Lib.$urlMap.appid,
                data: {}
            })
            res.then(function (res) {
                Store.commit('setLoginStatus',0)
                Store.commit('setAppId',res.data.appid)
                location.reload()
            })
            return
        }

        const appID=Store.getters.appId
        // 微信未授权登录跳转到授权登录页面
        const url = window.location.href

        // 解决重复登录url添加重复的code与state问题
        const parseUrl = qs.parse(url.split('?')[1])
        let loginUrl
        if (parseUrl.code && parseUrl.state) {
            delete parseUrl.code
            delete parseUrl.state
            loginUrl = `${url.split('?')[0]}?${qs.stringify(parseUrl)}`
        } else {
            loginUrl = url
        }
        // 设置微信授权回调地址
        let redirect_uri = loginUrl
        redirect_uri = encodeURIComponent(redirect_uri)
        let authUrl=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
        
        // 无论拒绝还是授权都设置成1

        // 跳转到微信授权页面
		Store.commit('setLoginStatus',1)
        window.location.href = authUrl
    }else if (Store.getters.login_Status == 1){
        let code = from.query.code
        if(code){
            let res = Lib.$http.post({
                url: Lib.$urlMap.authUser,
                data: {
                    code: code
                }
            })
            res.then(function (res) {
                if(res.code == 1){
                    let expTime = (new Date()).getTime() + config.EXPIRE*60*60*1000
                    res.data.expTime = expTime
                    Store.commit('setUserInfo',res.data)
                    Store.commit('setLoginStatus',2)
                    next()
                }else{
                    Store.commit('setLoginStatus',0)
                    location.reload()
                }
            })
        }else{
            Store.commit('setLoginStatus',0)
            location.reload()
        }
    }else if (Store.getters.login_Status == 2){
        if(Store.getters.user_Info.expTime && (new Date()).getTime() < Store.getters.user_Info.expTime){
            let Info = Store.getters.user_Info
            let expTime = (new Date()).getTime() + config.EXPIRE*60*60*1000
            // vm.$set(Info,'expTime',expTime)
            Info.expTime = expTime
            Store.commit('setUserInfo',Info)
            next();
        }else{
			console.log('授权过期')
            Store.commit('setLoginStatus',0)
            location.reload()
        }
    }

    // next();

})
router.afterEach((to, from) => {

})


export default RouterMount;
